import Vue from 'vue'
import { loginApi, getInfoApi, userIndexApi } from '@/api/user'
import { ACCESS_TOKEN, HAS_BANK, USER_NAME, REAL_NAME } from '../mutation-types'

const user = {
  namespaced: true,
  state: {
    token: "",
    name: "",
    hasbank: false,
    mount: {
      waitOrderAmount: 0,
      reapAmount: 0,
      waitReap: 0
    },
    readmsg: 0,
    level: "",
    avatar: "",
    realname: '',
    info: {}
  },
  getters: {
    isRealauth: (state) => {
      return state.info.card_id ? true : false
    },
    amount_total: (state) => {
      return state.info.price
    },
    bank: (state) => {
      return {
        accname: state.info.accname,
        bankname: state.info.bankname,
        bankid: state.info.bankid,
        banksite: state.info.banksite
      }
    },
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_INFO: (state, info) => {
      state.info = info
    },
    SET_BANK: (state, hasbank) => {
      state.hasbank = hasbank
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_REALNAME: (state, name) => {
      state.realname = name
    },
    SET_BASEINFO: (state, baseinfo) => {
      state.level = baseinfo.level;
      state.avatar = baseinfo.head
    },
    SET_READMSG: (state, readmsg) => {
      state.readmsg = readmsg;
    },
    SET_MOUNT: (state, mount) => {
      Object.assign(state.mount, mount)
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        loginApi(userInfo).then(res => {
          if (res.code === 200) {
            Vue.ls.set(ACCESS_TOKEN, res.data.token, 7 * 24 * 60 * 60 * 1000)
            Vue.ls.set(USER_NAME, res.data.username, 7 * 24 * 60 * 60 * 1000)
            commit('SET_TOKEN', res.data.token)
            commit('SET_NAME', res.data.username)

            resolve(res)
          } else {
            reject(res)
          }

        })
      })
    },
    GetBaseInfo({ commit }) {
      return new Promise((resolve, reject) => {
        userIndexApi().then(res => {
          commit('SET_BASEINFO', res.data.user)
          commit('SET_READMSG', res.data.readMsg)

          delete res.data.user
          commit('SET_MOUNT', res.data)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit }) {
      return new Promise((resolve, reject) => {
        getInfoApi().then(res => {
          if (res.code == 200) {
            commit('SET_INFO', res.data)
            commit('SET_REALNAME', res.data.name)
            Vue.ls.set(USER_NAME, res.data.username, 7 * 24 * 60 * 60 * 1000)
            commit('SET_NAME', res.data.username)
            Vue.ls.set(REAL_NAME, res.data.name, 7 * 24 * 60 * 60 * 1000)
            if (res.data.accname) {
              Vue.ls.set(HAS_BANK, 'hasBank', 7 * 24 * 60 * 60 * 1000)
            }
          }

          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    Logout({ commit, state }) { // 清控信息
      return new Promise((resolve) => {
        commit('SET_TOKEN', '')
        commit('SET_BANK', false)
        commit('SET_NAME', '')
        commit('SET_READMSG', 0)
        Vue.ls.remove(ACCESS_TOKEN)
        Vue.ls.remove(HAS_BANK)
        Vue.ls.remove(USER_NAME)
        resolve()
      })
    }

  }
}

export default user
